#ifndef USERMANAGEMENT_H
#define USERMANAGEMENT_H

#include "sqlmanagement.h"
#include "Logger.hpp"
#include <jsoncpp/json/json.h>
#include <QVariantMap>

class UserManagementBase {
public:
    UserManagementBase(SQLManagement* sqlManager, const QString& tableName)
        : sql_m(sqlManager), tb(tableName) {}

    virtual bool Insert(const Json::Value &userInfo) {
        QString query = QString("INSERT INTO %1 (username, password, identity, age, gender, phone, address) VALUES ('%2', '%3', '%4', %5, '%6', '%7', '%8')")
            .arg(tb)
            .arg(QString::fromStdString(userInfo["username"].asString()))
            .arg(QString::fromStdString(userInfo["password"].asString()))
            .arg(QString::fromStdString(userInfo["identity"].asString()))
            .arg(userInfo["age"].asInt()) // 整数类型
            .arg(QString::fromStdString(userInfo["gender"].asString()))
            .arg(QString::fromStdString(userInfo["phone"].asString()))
            .arg(QString::fromStdString(userInfo["address"].asString()));

        return sql_m->executeQuery(query);
    }

    virtual bool Remove(const QString &userId) {
        QString query = QString("DELETE FROM " + tb + " WHERE id = %1").arg(userId);
        return sql_m->executeQuery(query);
    }

    virtual bool Update(const Json::Value &json_value) {
        QString userId = QString::fromStdString(json_value["id"].asString());
        if (userId.isEmpty()) {
            LOG_DEBUG() << "userid is empty";
        }

        QString username = QString::fromStdString(json_value["username"].asString());
        QString password = QString::fromStdString(json_value["password"].asString());
        QString age = QString::fromStdString(json_value["age"].asString());
        QString gender = QString::fromStdString(json_value["gender"].asString());
        QString phone = QString::fromStdString(json_value["phone"].asString());
        QString address = QString::fromStdString(json_value["address"].asString());

        QString query = QString("UPDATE " + tb + " SET username = '%1', password = '%2', age = '%3', gender = '%4', phone = '%5', address = '%6' WHERE id = %7")
                            .arg(username).arg(password).arg(age).arg(gender).arg(phone).arg(address).arg(userId);
        return sql_m->executeQuery(query);
    }

    virtual QString GetIdByName(const QString &username) {
        QString query = QString("SELECT id FROM " + tb + " WHERE username = '%1'").arg(username);
        QVariant result = sql_m->executeQuerySingleResult(query);

        if (result.isValid()) {
            return result.toString();
        }

        LOG_ERROR() << "userid not exist";
        return "";
    }

    virtual QVariantMap GetInfo(const QString &userId) {
        QString query = QString("SELECT username, age, gender, phone, address FROM " + tb + " WHERE id = '%1'").arg(userId);
        QVariantMap result = sql_m->executeQueryResult(query);

        return result; // 用户信息不存在时返回空的 QVariantMap
    }

    virtual bool isExist(const QString &userId) {
        QString query = QString("SELECT COUNT(*) FROM " + tb + " WHERE id = '%1'").arg(userId);
        QVariant result = sql_m->executeQuerySingleResult(query);

        return result.isValid() && result.toInt() > 0; // 用户存在
    }

protected:
    SQLManagement* sql_m;
    QString tb; // 表名
};

class PatientManagement : public UserManagementBase {
public:
    PatientManagement(SQLManagement* sqlManager) : UserManagementBase(sqlManager, "patient_tb") {}
};

class DoctorManagement : public UserManagementBase {
public:
    DoctorManagement(SQLManagement* sqlManager) : UserManagementBase(sqlManager, "doctor_tb") {}
};

#endif // USERMANAGEMENT_H
